package cc.rengu.igas.momp.core.service.trans;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.dao.McMchntInfoMapper;
import cc.rengu.igas.momp.common.dao.MchntInfoMapper;
import cc.rengu.igas.momp.common.dao.PlatBizTmpMapper;
import cc.rengu.igas.momp.common.dao.impl.McMchntInfoMapperImpl;
import cc.rengu.igas.momp.common.dao.impl.MchntInfoMapperImpl;
import cc.rengu.igas.momp.common.dao.impl.PlatBizTmpMapperImpl;
import cc.rengu.igas.momp.common.entity.*;
import cc.rengu.igas.momp.common.enums.*;
import cc.rengu.igas.momp.common.model.bean.JsonTmpCpMcInfo;
import cc.rengu.igas.momp.common.model.bean.JsonTmpMcMchntInfo;
import cc.rengu.igas.momp.common.util.JsonOperation;
import cc.rengu.igas.momp.facade.bean.CouponMcInfoBean;
import cc.rengu.igas.momp.facade.bean.McMchntInfoBean;
import cc.rengu.igas.momp.facade.bean.MoneyOffMcListInfoBean;
import cc.rengu.igas.momp.facade.request.QueryMcMerListRequest;
import cc.rengu.igas.momp.facade.response.QueryMcMerListInfoResponse;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 营销活动商户/用户列表查询功能实现
 *
 * @author liujinan
 * @date 2021/3/22 17:26
 */
public class QueryMcMerListService extends RadpService {


    /**
     * 法人机构号
     */
    private String globalInstId;
    /**
     * 操作类型
     */
    private String globalQueryType;
    /**
     * 操作人
     */
    private String globalQueryId;
    /**
     * 操作角色
     */
    private String globalQueryRole;
    /**
     * 操作人所属机构
     */
    private String globalQueryOrgId;
    /**
     * 当前系统日期
     */
    private String globalTimeStamp;
    /**
     * 营销活动编号
     */
    private String globalMcNo;
    /**
     * 商户类型
     */
    private String globalMchntType;

    @Override
    protected int bizInit(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizProcess(String s, int i) {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        try {

            QueryMcMerListRequest queryMcMerListRequest = new QueryMcMerListRequest();
            ConvertUtil.convertOutput(queryMcMerListRequest);

            QueryMcMerListInfoResponse queryMcMerListInfoResponse = new QueryMcMerListInfoResponse();
            queryMcMerListInfoResponse.setHeader(queryMcMerListRequest.getHeader());
            BizResponse<QueryMcMerListInfoResponse> bizResponse = new BizResponse<>();
            bizResponse.setResult(queryMcMerListInfoResponse);

            /* 报文检查 */
            messageValidation(queryMcMerListRequest);

            rglog.info("机构<{}>在<{}>由<{}>开始查询营销活动商戶/用戶列表信息流程", globalInstId, globalTimeStamp, globalQueryId);

            // 服务调用
            BizResponse<QueryMcMerListInfoResponse> bizResponseNew = queryMcMerUserList(xmlTreeUtil, queryMcMerListRequest, queryMcMerListInfoResponse);

            ConvertUtil.convertInput(bizResponseNew.getResult());

        } catch (BizException e) {
            rglog.error(StringUtil.ExceptionToString(e));
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, e.getCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizTerminate(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 查询营销活动商戶、用戶列表信息
     *
     * @param xmlTreeUtil                内部XML树
     * @param queryMcMerListRequest      营销活动商戶、用戶列表查询接口请求对象
     * @param queryMcMerListInfoResponse 营销活动商戶、用戶d列表查询接口应答对象
     * @return cc.rengu.oltp.service.model.BizResponse<cc.rengu.igas.momp.facade.response.QueryMcMerListInfoResponse> 营销活动商戶、用戶列表查询接口应答对象
     * @throws BizException 业务异常信息
     * @author Jinan Liu
     * @date 2020/5/27 22:41
     */
    private BizResponse<QueryMcMerListInfoResponse> queryMcMerUserList(XmlTreeUtil xmlTreeUtil, QueryMcMerListRequest queryMcMerListRequest, QueryMcMerListInfoResponse queryMcMerListInfoResponse)
            throws Exception {

        BizResponse<QueryMcMerListInfoResponse> bizResponse = new BizResponse<>();

        List<MoneyOffMcListInfoBean> moneyOffMcListInfoBeanList;
        //营销活动类型
        rglog.debug("当前是<{}>查询数据", QueryRoleEnum.getByValue(globalQueryRole).getQueryRoleDesc());
        PageInfo pageInfo = new PageInfo();
        String mcType = globalMcNo.substring(2, 4);

        /** 商户列表信息查询 */
        List<Object> mcMchntInfoList = new ArrayList<Object>();

        List<McMchntInfoBean> mcMchntInfoBeanList = new ArrayList<>();

        /** 减免类 */
        if (McTypeEnum.REDUCTION_FIXED_AMOUNT.getMcTypeCode().equals(mcType) || McTypeEnum.REDUCTION_RANDOM_AMOUNT.getMcTypeCode().equals(mcType) ||
                McTypeEnum.REDUCTION_FIXED_RATE.getMcTypeCode().equals(mcType) || McTypeEnum.REDUCTION_RANDOM_RATE.getMcTypeCode().equals(mcType)) {

            switch (QueryTypeEnum.getByValue(globalQueryType)) {
                case BANK_QUERY_TYPE:
                    /* 银行人员查询 */
                    mcMchntInfoList = getAllMcMchntListInfo();
                    if (null != mcMchntInfoList && !mcMchntInfoList.isEmpty()) {
                        mcMchntInfoBeanList = mcMchntInfoList.stream().map(this::convertMcMchntInfo).collect(Collectors.toList());
                    }
                    break;
                case MCHNT_QYERY_TYPE:
                    /* 商户信息查询 */
                    mcMchntInfoList = getAllMcMchntListInfo();
                    if (null != mcMchntInfoList && !mcMchntInfoList.isEmpty()) {
                        mcMchntInfoBeanList = mcMchntInfoList.stream().map(this::convertMcMchntInfo).collect(Collectors.toList());
                    }
                    break;

                default:
                    /* 其他查询角色不支持 */
                    rglog.error("当前<{}>角色的查询用户<{}>无权限操作数据!", globalQueryRole, globalQueryType);
                    throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespDesc());
            }

            /** 卡券类 */
        } else if (McTypeEnum.COUPON_ALL.getMcTypeCode().equals(mcType) || McTypeEnum.COUPON_FREE.getMcTypeCode().equals(mcType) ||
                McTypeEnum.COUPON_PAID.getMcTypeCode().equals(mcType)) {

            /* 判断查询类型 */
            CpMcListInfoInObj cpMcListInfoInObj = new CpMcListInfoInObj();
            /* 机构编码 */
            cpMcListInfoInObj.setInstId(globalInstId);
            /* 营销活动编号 */
            cpMcListInfoInObj.setMcNo(globalMcNo);

            /* 根据角色权限查询数据 */
            switch (QueryTypeEnum.getByValue(globalQueryType)) {
                case BANK_QUERY_TYPE:
                    /* 银行人员,需要拼接营销活动机构信息表查询 */

                    /* 机构编号 */
                    if (!StringUtil.isNullorEmpty(globalQueryOrgId)) {
                        cpMcListInfoInObj.setOrgId(globalQueryOrgId);
                    }

                    /* 银行人员查询营销活动详细信息 */
                    if (QueryRoleEnum.BANK_BASIC_QUERY_ROLE.getQueryRoleCode().equals(globalQueryRole) ||
                            QueryRoleEnum.BANK_PUBLISH_QYERY_ROLE.getQueryRoleCode().equals(globalQueryRole)) {
                        /* 查询营销活动商户信息 */
                        mcMchntInfoList = getAllMcMchntListInfo();
                        if (null != mcMchntInfoList && !mcMchntInfoList.isEmpty()) {
                            mcMchntInfoBeanList = mcMchntInfoList.stream().map(this::convertMcMchntInfo).collect(Collectors.toList());
                        }

                    } else if (QueryRoleEnum.BANK_AUDIT_QYERY_ROLE.getQueryRoleCode().equals(globalQueryRole)) {
                        /* 银行审批岗流程 */
                        TMPlatBizTmp tmPlatBizTmp = selectCouponMcDetailInfoByBankAuditRole(cpMcListInfoInObj);
                        if (null == tmPlatBizTmp) {
                            rglog.error("机构<{}>的卡券类营销活动<{}>的基本信息查询失败!用户无权限查看数据或不存在该营销活动", globalInstId, globalMcNo);
                            throw new BizException(RespCodeEnum.GET_MONEY_OFF_MC_DETAIL_ERROR.getRespCode(), RespCodeEnum.GET_MONEY_OFF_MC_DETAIL_ERROR.getRespDesc());
                        }
                        MoMcListInfoInObj moMcListInfoInObj = new MoMcListInfoInObj();
                        moMcListInfoInObj.setInstId(globalInstId);
                        moMcListInfoInObj.setMcNo(globalMcNo);

                        List<TMPlatBizTmp> tmPlatBizTmpList = getAllMcTmpInfo(moMcListInfoInObj);
                        /* 查询营销活动商户信息 */
                        List<TMPlatBizTmp> tmPlatBizTmpMchntList = tmPlatBizTmpList.stream().filter(record -> TableNameEnum.T_M_MC_MCHNT_INFO.getTableNameCode().equals(record.getOprTableEname())).collect(Collectors.toList());
                        if (null != tmPlatBizTmpMchntList && !tmPlatBizTmpMchntList.isEmpty()) {
                            mcMchntInfoBeanList = tmPlatBizTmpMchntList.stream().map(this::convertTmpMcMchntInfo).collect(Collectors.toList());
                            /* 处理商户、商圈类型 */
                            rglog.info("机构<{}>的卡券类营销活动<{}>查询临时表商户列表size<{}>", globalInstId, globalMcNo, mcMchntInfoBeanList.size());
                            mcMchntInfoBeanList = mcMchntInfoBeanList.stream().filter(item -> globalMchntType.equals(item.getMchntType())).collect(Collectors.toList());
                            rglog.info("机构<{}>的卡券类营销活动<{}>商户列表筛选商户类型<{}>[0商户1商圈]，筛选后size<{}>", globalInstId, globalMcNo, globalMchntType, mcMchntInfoBeanList.size());
                        }

                    } else {
                        rglog.error("当前<{}>角色的查询用户<{}>无权限操作数据!", globalQueryRole, globalQueryType);
                        throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespDesc());
                    }
                    break;
                case MCHNT_QYERY_TYPE:
                    /* 商户人员 */
                    String mchntNo = globalQueryId;
                    TBMchntBaseInfo tbMchntBaseInfo = null;
                    if (!StringUtil.isNullorEmpty(mchntNo)) {
                        /* 校验商户信息 */
                        tbMchntBaseInfo = selectMompMchntBaseInfoByPrimaryKey(globalInstId, mchntNo);
                        if (null == tbMchntBaseInfo) {
                            rglog.error("查询法人机构<{}>商户<{}>基本信息失败!", globalInstId, mchntNo);
                            throw new BizException(RespCodeEnum.NO_TRANS_RECORD.getRespCode(), RespCodeEnum.NO_TRANS_RECORD.getRespDesc());
                        }
                    }
                    /* 查询营销活动商户列表信息 */
                    mcMchntInfoList = getAllMcMchntListInfo();
                    if (null != mcMchntInfoList && !mcMchntInfoList.isEmpty()) {
                        mcMchntInfoBeanList = mcMchntInfoList.stream().map(this::convertMcMchntInfo).collect(Collectors.toList());
                    }
                    break;
                default:
                    /* 其他查询角色不支持 */
                    rglog.error("当前<{}>角色的查询用户<{}>无权限操作数据!", globalQueryRole, globalQueryType);
                    throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespDesc());
            }

            /** 手续费类 */
        } else if (McTypeEnum.MCHNT_FEE_DISCOUNT.getMcTypeCode().equals(mcType)) {

            if (QueryTypeEnum.BANK_QUERY_TYPE.getQueryTypeCode().equals(globalQueryType)) {
                /* 银行人员查询 */
                if (QueryRoleEnum.BANK_BASIC_QUERY_ROLE.getQueryRoleCode().equals(globalQueryRole) ||
                        QueryRoleEnum.BANK_PUBLISH_QYERY_ROLE.getQueryRoleCode().equals(globalQueryRole)) {
                    /* 查询营销活动商户信息 */
                    mcMchntInfoList = getAllMcMchntListInfo();
                    if (null != mcMchntInfoList && !mcMchntInfoList.isEmpty()) {
                        mcMchntInfoBeanList = mcMchntInfoList.stream().map(this::convertMcMchntInfo).collect(Collectors.toList());
                    }

                    /* 银行审批岗查询 */
                } else if (QueryRoleEnum.BANK_AUDIT_QYERY_ROLE.getQueryRoleCode().equals(globalQueryRole)) {
                    /* 取临时表中所有数据 */
                    MoMcListInfoInObj moMcListInfoInObj = new MoMcListInfoInObj();
                    moMcListInfoInObj.setInstId(globalInstId);
                    moMcListInfoInObj.setMcNo(globalMcNo);

                    List<TMPlatBizTmp> tmPlatBizTmpList = getAllMcTmpInfo(moMcListInfoInObj);
                    if (null != tmPlatBizTmpList && !tmPlatBizTmpList.isEmpty()) {
                        List<TMPlatBizTmp> tmPlatBizTmpMchntList = tmPlatBizTmpList.stream().filter(record -> TableNameEnum.T_M_MC_MCHNT_INFO.getTableNameCode().equals(record.getOprTableEname())).collect(Collectors.toList());
                        mcMchntInfoBeanList = tmPlatBizTmpMchntList.stream().map(this::convertTmpMcMchntInfo).collect(Collectors.toList());
                        /* 临时表区分商户、商圈数据 */
                        rglog.info("机构<{}>的卡券类营销活动<{}>查询临时表商户列表size<{}>", globalInstId, globalMcNo, mcMchntInfoBeanList.size());
                        mcMchntInfoBeanList = mcMchntInfoBeanList.stream().filter(item -> globalMchntType.equals(item.getMchntType())).collect(Collectors.toList());
                        rglog.info("机构<{}>的卡券类营销活动<{}>商户列表筛选商户类型<{}>[0商户1商圈]，筛选后size<{}>", globalInstId, globalMcNo, globalMchntType, mcMchntInfoBeanList.size());
                    }

                } else {
                    /* 其他查询角色不支持 */
                    rglog.error("当前<{}>角色的查询用户<{}>无权限操作数据!", globalQueryRole, globalQueryType);
                    throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespDesc());
                }
            } else {
                /* 其他查询角色不支持 */
                rglog.error("当前<{}>角色的查询用户<{}>无权限操作数据!", globalQueryRole, globalQueryType);
                throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespDesc());
            }
            /** 代理商 */
        } else if (McTypeEnum.AGENT_CASHBACK.getMcTypeCode().equals(mcType)) {
            rglog.error("营销活动编号上送错误!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            /** 收银员 */
        } else if (McTypeEnum.CASHER_CASHBACK.getMcTypeCode().equals(mcType)) {
            /* 判断查询类型 */
            CcbMcListInfoInObj ccbMcListInfoInObj = new CcbMcListInfoInObj();
            /* 机构编码 */
            ccbMcListInfoInObj.setInstId(globalInstId);
            /* 营销活动编号 */
            ccbMcListInfoInObj.setMcNo(globalMcNo);
            /* 机构编号 */
            if (!StringUtil.isNullorEmpty(globalQueryOrgId)) {
                ccbMcListInfoInObj.setOrgId(globalQueryOrgId);
            }

            /* 校验查询类型权限 */
            if (QueryTypeEnum.BANK_QUERY_TYPE.getQueryTypeCode().equals(globalQueryType) ||
                    QueryTypeEnum.MCHNT_QYERY_TYPE.getQueryTypeCode().equals(globalQueryType) ||
                    QueryTypeEnum.CASHIER_QYERY_TYPE.getQueryTypeCode().equals(globalQueryType)) {
                /* 根据角色权限查询数据 */
                if (QueryRoleEnum.BANK_BASIC_QUERY_ROLE.getQueryRoleCode().equals(globalQueryRole) ||
                        QueryRoleEnum.BANK_PUBLISH_QYERY_ROLE.getQueryRoleCode().equals(globalQueryRole) ||
                        QueryRoleEnum.MCHNT_QYERY_ROLE.getQueryRoleCode().equals(globalQueryRole) ||
                        QueryRoleEnum.CASHIER_QYERY_ROLE.getQueryRoleCode().equals(globalQueryRole)) {

                    /* 查询营销活动商户信息 */
                    mcMchntInfoList = getAllMcMchntListInfo();
                    if (null != mcMchntInfoList && !mcMchntInfoList.isEmpty()) {
                        mcMchntInfoBeanList = mcMchntInfoList.stream().map(this::convertMcMchntInfo).collect(Collectors.toList());
                    }

                    //银行审批岗
                } else if (QueryRoleEnum.BANK_AUDIT_QYERY_ROLE.getQueryRoleCode().equals(globalQueryRole)) {

                    /* 取临时表中所有数据 */
                    CcbMcListInfoInObj ccbMcListInfoInObj1 = new CcbMcListInfoInObj();
                    ccbMcListInfoInObj1.setInstId(globalInstId);
                    ccbMcListInfoInObj1.setMcNo(globalMcNo);
                    List<TMPlatBizTmp> tmPlatBizTmpList = getAllMcTmpInfo(ccbMcListInfoInObj1);
                    if (null != tmPlatBizTmpList && !tmPlatBizTmpList.isEmpty()) {
                        /* 查询营销活动商户信息 */
                        List<TMPlatBizTmp> tmPlatBizTmpMchntList = tmPlatBizTmpList.stream().filter(record -> TableNameEnum.T_M_MC_MCHNT_INFO.getTableNameCode().equals(record.getOprTableEname())).collect(Collectors.toList());
                        mcMchntInfoBeanList = tmPlatBizTmpMchntList.stream().map(this::convertTmpMcMchntInfo).collect(Collectors.toList());
                        /* 临时表区分商户、商圈数据 */
                        rglog.info("机构<{}>的卡券类营销活动<{}>查询临时表商户列表size<{}>", globalInstId, globalMcNo, mcMchntInfoBeanList.size());
                        mcMchntInfoBeanList = mcMchntInfoBeanList.stream().filter(item -> globalMchntType.equals(item.getMchntType())).collect(Collectors.toList());
                        rglog.info("机构<{}>的卡券类营销活动<{}>商户列表筛选商户类型<{}>[0商户1商圈]，筛选后size<{}>", globalInstId, globalMcNo, globalMchntType, mcMchntInfoBeanList.size());
                    }

                } else {
                    /* 其他查询角色不支持 */
                    rglog.error("当前<{}>角色的查询用户<{}>无权限操作数据!", globalQueryRole, globalQueryType);
                    throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespDesc());
                }
            } else {
                /* 其他查询角色不支持 */
                rglog.error("当前<{}>角色的查询用户<{}>无权限操作数据!", globalQueryRole, globalQueryType);
                throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespDesc());
            }

        } else {
            rglog.error("营销活动类型上送错误!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        StringBuffer mchntNos = new StringBuffer();
        for (McMchntInfoBean mcMchntInfoBean : mcMchntInfoBeanList) {
            mchntNos.append(mcMchntInfoBean.getMchntNo()).append(",");
        }
        String mchntNo = mchntNos.toString();
        if (mchntNo.length() > 0) {
            mchntNo = mchntNo.substring(0, mchntNo.length() - 1);
        }
        queryMcMerListInfoResponse.setMchntNos(mchntNo);

        bizResponse.setRspSysId(ChannelEnum.MOMP.getChannelType());
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        queryMcMerListInfoResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        queryMcMerListInfoResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        bizResponse.setResult(queryMcMerListInfoResponse);

        return bizResponse;
    }

    /**
     * 请求报文体内容检查
     *
     * @param queryMcMerListRequest 减免类活动列表查询接口请求对象
     * @author Jinan Liu
     * @date 2020/6/20 09:55
     */
    private void messageValidation(QueryMcMerListRequest queryMcMerListRequest) {

        if (StringUtil.isNullorEmpty(queryMcMerListRequest.getHeader().getTransDate())) {
            rglog.error("交易日期必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        if (StringUtil.isNullorEmpty(queryMcMerListRequest.getHeader().getTransTime())) {
            rglog.error("交易时间必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        if (StringUtil.isNullorEmpty(queryMcMerListRequest.getHeader().getInstId())) {
            rglog.error("法人机构号必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalInstId = queryMcMerListRequest.getHeader().getInstId();
        }

        if (StringUtil.isNullorEmpty(queryMcMerListRequest.getHeader().getTraceNo())) {
            rglog.error("流水号必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        if (StringUtil.isNullorEmpty(queryMcMerListRequest.getQueryType())) {
            rglog.error("操作员类型为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalQueryType = queryMcMerListRequest.getQueryType();
        }

        if (StringUtil.isNullorEmpty(queryMcMerListRequest.getQueryId())) {
            rglog.error("操作员ID为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalQueryId = queryMcMerListRequest.getQueryId();
        }

        if (StringUtil.isNullorEmpty(queryMcMerListRequest.getQueryRole())) {
            rglog.error("操作员角色为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalQueryRole = queryMcMerListRequest.getQueryRole();
        }

        if (StringUtil.isNullorEmpty(queryMcMerListRequest.getQueryOrgId())) {
            rglog.error("操作员所属法人机构为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalQueryOrgId = queryMcMerListRequest.getQueryOrgId();
        }

        if (StringUtil.isNullorEmpty(queryMcMerListRequest.getMcNo())) {
            rglog.error("营销活动编号为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalMcNo = queryMcMerListRequest.getMcNo();
        }
        /** 商户类型 */
        globalMchntType = CommonConstant.ZERO_COMMON_CONSTANT;

        globalTimeStamp = new SimpleDateFormat(CommonConstant.TIMESTAMP_FORMAT).format(new Date());
    }

    private TMPlatBizTmp selectCouponMcDetailInfoByBankAuditRole(CpMcListInfoInObj cpMcListInfoInObj) throws Exception {
        PlatBizTmpMapper platBizTmpMapper = new PlatBizTmpMapperImpl();
        return platBizTmpMapper.selectCouponMcDetailInfoByBankAuditRole(cpMcListInfoInObj);
    }

    /**
     * 使用商户编号从内管商户基本信息表查询商户所属商圈编号
     *
     * @param globalInstId 法人机构号
     * @param mchntNo      商户编号
     * @return cc.rengu.igas.momp.common.entity.TBMchntBaseInfo 商户基本信息表T_B_MCHNT_BASE_INFO
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2021/3/25 13:06
     */
    private TBMchntBaseInfo selectMompMchntBaseInfoByPrimaryKey(String globalInstId, String mchntNo) throws Exception {
        MchntInfoMapper mchntInfoMapper = new MchntInfoMapperImpl();
        return mchntInfoMapper.selectMompMchntBaseInfoByPrimaryKey(globalInstId, mchntNo);
    }


    /**
     * 获取营销活动商户信息列表(正式表)
     *
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMMcMchntInfo> 营销活动商户信息表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 18:35
     */
    private List<Object> getAllMcMchntListInfo() throws Exception {
        List<Object> tmMcMchntInfoList = new ArrayList<>();
        PageInfo pageInfo = new PageInfo();
        int i = 0;
        pageInfo.setPageIndex(i);
        pageInfo.setPageSize(9999);
        TMMcMchntInfo tmMcMchntInfo = new TMMcMchntInfo();
        tmMcMchntInfo.setInstId(globalInstId);
        tmMcMchntInfo.setMcNo(globalMcNo);
        tmMcMchntInfo.setMchntType(globalMchntType);

        McMchntInfoMapper mcMchntInfoMapper = new McMchntInfoMapperImpl();
        PageResult tmMcMchntInfoPageInfo = mcMchntInfoMapper.selectMcMchntInfoList(tmMcMchntInfo, pageInfo);
        if (null != tmMcMchntInfoPageInfo && null != tmMcMchntInfoPageInfo.getResult()) {
            tmMcMchntInfoList.addAll(tmMcMchntInfoPageInfo.getResult());
        }
        while (null != tmMcMchntInfoPageInfo && null != tmMcMchntInfoPageInfo.getResult() && tmMcMchntInfoPageInfo.getResult().size() >= 1 && i < 6) {
            i++;
            pageInfo.setPageIndex(i);
            tmMcMchntInfoPageInfo = mcMchntInfoMapper.selectMcMchntInfoList(tmMcMchntInfo, pageInfo);
            if (null != tmMcMchntInfoPageInfo && null != tmMcMchntInfoPageInfo.getResult()) {
                tmMcMchntInfoList.addAll(tmMcMchntInfoPageInfo.getResult());
            }
        }
        return tmMcMchntInfoList;
    }

    /**
     * 卡券类营销活动基本信息数据库临时表实体类和接口实体类数据转换
     *
     * @param tmPlatBizTmp 业务数据临时表
     * @return cc.rengu.igas.momp.facade.bean.CouponMcInfoBean 卡券类活动基本信息对象
     * @author Jinan Liu
     * @date 2020/5/28 20:35
     */
    private CouponMcInfoBean convertTmpMcInfo(TMPlatBizTmp tmPlatBizTmp) {
        CouponMcInfoBean couponMcInfoBean = new CouponMcInfoBean();
        try {

            JsonTmpCpMcInfo jsonTmpCpMcInfo = JsonOperation.parseCpMcInfoTmpBizData1(tmPlatBizTmp, rglog);

            /* 营销活动编号 */
            couponMcInfoBean.setMcNo(jsonTmpCpMcInfo.getMcNo());
            /* 营销活动名称 */
            couponMcInfoBean.setMcName(jsonTmpCpMcInfo.getMcName());
            /* 营销活动类型 */
            couponMcInfoBean.setMcType(jsonTmpCpMcInfo.getMcType());
            /* 营销活动状态 */
            couponMcInfoBean.setMcStatus(jsonTmpCpMcInfo.getMcStatus());
            /* 活动起始日期 */
            couponMcInfoBean.setMcStartDate(jsonTmpCpMcInfo.getMcStartDate());
            /* 活动截止日期 */
            couponMcInfoBean.setMcEndDate(jsonTmpCpMcInfo.getMcEndDate());
            /* 活动结束日期 */
            couponMcInfoBean.setMcActualEndDate(jsonTmpCpMcInfo.getMcActualEndDate());
            /* 活动时间标志 */
            couponMcInfoBean.setMcTimeFlag(jsonTmpCpMcInfo.getMcTimeFlag());
            /* 出资方 */
            couponMcInfoBean.setSponsor(jsonTmpCpMcInfo.getSponsor());
            /* 参与次数限制 */
            couponMcInfoBean.setRestriction(jsonTmpCpMcInfo.getRestriction());
            /* 客户范围 */
            couponMcInfoBean.setClientRange(jsonTmpCpMcInfo.getClientRange());
            /* 商户范围 */
            couponMcInfoBean.setMerchantRange(jsonTmpCpMcInfo.getMerchantRange());
            /* 新增商户活动标志 */
            couponMcInfoBean.setNewMchntFlag(jsonTmpCpMcInfo.getNewMchntFlag());
            /* 限制金额下限 */
            couponMcInfoBean.setAmtLimitFloor(jsonTmpCpMcInfo.getAmtLimitFloor());
            /* 限制金额上限 */
            couponMcInfoBean.setAmtLimitCelling(jsonTmpCpMcInfo.getAmtLimitCelling());
            /* 预算充值方式 */
            couponMcInfoBean.setBudgetRechargeMethod(jsonTmpCpMcInfo.getBudgetRechargeMethod());
            /* 预算 */
            couponMcInfoBean.setBudget(jsonTmpCpMcInfo.getBudget());
            /* 银行出资预算 */
            couponMcInfoBean.setBankBudget(jsonTmpCpMcInfo.getBankBudget());
            /* 预期参与商户数量 */
            couponMcInfoBean.setExpectMchntQty(jsonTmpCpMcInfo.getExpectMchntQty());
            /* 银行出资比例 */
            couponMcInfoBean.setBankBudgetRatio(jsonTmpCpMcInfo.getBankBudgetRatio());
            /* 购买标志 */
            couponMcInfoBean.setPurchaseFlag(jsonTmpCpMcInfo.getPurchaseFlag());
            /* 卡券生效时间 */
            couponMcInfoBean.setAvailableDate(jsonTmpCpMcInfo.getAvailableDate());
            /* 卡券有效期标志 */
            couponMcInfoBean.setCouponDateFlag(jsonTmpCpMcInfo.getCouponDateFlag());
            /* 卡券有效起始日期 */
            couponMcInfoBean.setCouponStartDate(jsonTmpCpMcInfo.getCouponStartDate());
            /* 卡券有效结束日期 */
            couponMcInfoBean.setCouponEndDate(jsonTmpCpMcInfo.getCouponEndDate());
            /* 卡券相对周期 */
            couponMcInfoBean.setCouponPeriod(jsonTmpCpMcInfo.getCouponPeriod());
            /* 卡券库存 */
            couponMcInfoBean.setCouponInventory(jsonTmpCpMcInfo.getCouponInventory());
            /* 卡券库存 */
            couponMcInfoBean.setCouponRest(jsonTmpCpMcInfo.getCouponRest());
            /* 卡券已领取张数 */
            couponMcInfoBean.setCouponClaimed(jsonTmpCpMcInfo.getCouponClaimed());
            /* 卡券已核销张数 */
            couponMcInfoBean.setCouponRedeemed(jsonTmpCpMcInfo.getCouponRedeemed());
            /* 审批状态   */
            couponMcInfoBean.setAuditStatus(tmPlatBizTmp.getAuditStatus());
            /* 创建人 */
            couponMcInfoBean.setOprId(tmPlatBizTmp.getOprId());
            /* 创建时间 */
            couponMcInfoBean.setCreateTime(tmPlatBizTmp.getCreateTime());
            /* 最后修改人 */
            couponMcInfoBean.setLastOprId(tmPlatBizTmp.getLastOprId());
            /* 修改时间 */
            couponMcInfoBean.setUpdateTime(tmPlatBizTmp.getLastUpdateTime());
            /* 审批拒绝原因 */
            couponMcInfoBean.setAuditRefuseReason(tmPlatBizTmp.getAuditRefuseReason());
            /* 备用字段1 */
            couponMcInfoBean.setRemark1(tmPlatBizTmp.getRemark1());
            /* 备用字段2 */
            couponMcInfoBean.setRemark2(tmPlatBizTmp.getRemark2());
            /* 备用字段3 */
            couponMcInfoBean.setRemark2(tmPlatBizTmp.getRemark3());
            /* 备用字段4 */
            couponMcInfoBean.setRemark2(tmPlatBizTmp.getRemark4());
            /* 备用字段5 */
            couponMcInfoBean.setRemark2(tmPlatBizTmp.getRemark5());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("转换营销活动信息异常,异常信息:<{}>", byteArrayOutputStream.toString());
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }

        return couponMcInfoBean;
    }


    /**
     * 营销活动商户信息数据库实体类和接口实体类数据转换
     *
     * @param object 营销活动商户信息表
     * @return cc.rengu.igas.momp.facade.bean.McMchntInfoBean 营销活动商户信息对象
     * @author Jinan Liu
     * @date 2020/4/1 10:33
     */
    private McMchntInfoBean convertMcMchntInfo(Object object) {
        McMchntInfoBean mcMchntInfoBean = new McMchntInfoBean();
        TMMcMchntInfo mcMchntInfo = (TMMcMchntInfo) object;
        try {
            /* 商户范围类型 */
            mcMchntInfoBean.setMchntType(mcMchntInfo.getMchntType());
            /* 商户范围号 */
            mcMchntInfoBean.setMchntNo(mcMchntInfo.getMchntNo());
            /* 商户范围名称 */
            mcMchntInfoBean.setMchntName(mcMchntInfo.getMchntName());
            /* 新增商户标志 */
            mcMchntInfoBean.setNewMchntFlag(mcMchntInfo.getNewMchntFlag());
            /* 退出标志 */
            mcMchntInfoBean.setExitFlag(mcMchntInfo.getExitFlag());
            /* 操作类型 */
            if ((CommonConstant.MCHNT_REFUSE_FLAG.equals(mcMchntInfo.getNewMchntFlag()) || CommonConstant.MCHNT_NEW_FLAG.equals(mcMchntInfo.getNewMchntFlag())) && CommonConstant.MC_EXIT_FLAG.equals(mcMchntInfo.getExitFlag())) {

                mcMchntInfoBean.setOprType(CommonConstant.BIZ_OPR_INSERT);
            } else {
                mcMchntInfoBean.setOprType(CommonConstant.EMPTY_STRING);
            }


        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("转换营销活动商户信息异常,异常信息:<{}>", byteArrayOutputStream.toString());
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }

        return mcMchntInfoBean;
    }

    /**
     * 银行审批岗获取指定营销活动所有待审批数据
     *
     * @param moMcListInfoInObj 营销活动列表查询使用实体类入参
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMPlatBizTmp> 业务数据临时表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 19:55
     */
    private List<TMPlatBizTmp> getAllMcTmpInfo(MoMcListInfoInObj moMcListInfoInObj) throws Exception {

        PlatBizTmpMapper platBizTmpMapper = new PlatBizTmpMapperImpl();
        return platBizTmpMapper.selectSingleMcAllDetailInfoByBankAuditRole(moMcListInfoInObj);
    }

    /**
     * 营销活动商户信息数据库临时表实体类和接口实体类数据转换
     *
     * @param tmPlatBizTmp 业务数据临时表
     * @return cc.rengu.igas.momp.facade.bean.McMchntInfoBean 营销活动商户信息对象
     * @author Jinan Liu
     * @date 2020/5/28 20:13
     */
    private McMchntInfoBean convertTmpMcMchntInfo(TMPlatBizTmp tmPlatBizTmp) {

        McMchntInfoBean mcMchntInfoBean = new McMchntInfoBean();
        try {

            JsonTmpMcMchntInfo jsonTmpMcMchntInfo = JsonOperation.parseTmpMcMchntInfoBizData1(tmPlatBizTmp, rglog);

            /* 商户范围类型 */
            mcMchntInfoBean.setMchntType(jsonTmpMcMchntInfo.getMchntType());
            /* 商户范围号 */
            mcMchntInfoBean.setMchntNo(jsonTmpMcMchntInfo.getMchntNo());
            /* 商户范围名称 */
            mcMchntInfoBean.setMchntName(jsonTmpMcMchntInfo.getMchntName());
            if (tmPlatBizTmp.getAuditStatus().equals(AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode()) || tmPlatBizTmp.getAuditStatus().equals(AuditStatusEnum.NEW_AUDIT_REJECT.getAuditStatusCode())
                    || tmPlatBizTmp.getAuditStatus().equals(AuditStatusEnum.PUBLISH_AUDIT_REJECT.getAuditStatusCode())) {
                /* 审批状态*/
                mcMchntInfoBean.setOprType(CommonConstant.BIZ_OPR_INSERT);
            }

        } catch (Exception e) {
            rglog.error("转换临时表营销活动机构信息异常,异常信息:<{}>", StringUtil.ExceptionToString(e));
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }

        return mcMchntInfoBean;
    }


    /**
     * 银行审批岗获取指定营销活动所有待审批数据
     *
     * @param ccbMcListInfoInObj 营销活动列表查询使用实体类入参
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMPlatBizTmp> 业务数据临时表
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2021/3/24 19:55
     */
    private List<TMPlatBizTmp> getAllMcTmpInfo(CcbMcListInfoInObj ccbMcListInfoInObj) throws Exception {

        PlatBizTmpMapper platBizTmpMapper = new PlatBizTmpMapperImpl();
        return platBizTmpMapper.selectCashierDetailInfoByBankAuditRole(ccbMcListInfoInObj);
    }

}
